﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RadioPlayerWindow.xaml.cs" company="">
//   
// </copyright>
// <summary>
//   Interaction logic for MediaPlayerWindow.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace TinyRadio.UserInterface.Views
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows;
    using System.Windows.Forms;
    using System.Windows.Resources;

    using MahApps.Metro.Controls;

    using Application = System.Windows.Application;

    /// <summary>
    ///     Interaction logic for MediaPlayerWindow.xaml
    /// </summary>
    public partial class RadioPlayerWindow : MetroWindow
    {
        #region Fields

        /// <summary>The _tray.</summary>
        private SysTrayIcon _tray;

        #endregion

        #region Constructors and Destructors

        /// <summary>Initializes a new instance of the <see cref="RadioPlayerWindow"/> class.</summary>
        public RadioPlayerWindow()
        {
            this.InitializeComponent();

            this.CreateSysTrayIcon();

            this.Closed += (s, e) =>
                {
                    this._tray.Dispose();
                    this._tray = null;
                };
        }

        #endregion

        #region Methods

        /// <summary>The on closing.</summary>
        /// <param name="e">The e.</param>
        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = true;

            this.Hide();
        }

        /// <summary>The on state changed.</summary>
        /// <param name="e">The e.</param>
        protected override void OnStateChanged(EventArgs e)
        {
            base.OnStateChanged(e);

            if (this.WindowState == WindowState.Minimized)
            {
                this.Hide();
            }
        }

        /// <summary>The create sys tray icon.</summary>
        /// <exception cref="ApplicationException"></exception>
        private void CreateSysTrayIcon()
        {
            var iconResourceUri = new Uri("pack://application:,,,/TinyRadio;component/Resources/Images/Radio.ico");

            StreamResourceInfo iconResource = Application.GetResourceStream(iconResourceUri);

            if (iconResource == null || iconResource.Stream == null)
            {
                throw new ApplicationException("Failed to load systray icon:" + iconResourceUri);
            }

            var icon = new Icon(iconResource.Stream);

            this._tray = new SysTrayIcon("Tiny Radio", icon).Add("Exit", (s, e) => Application.Current.Shutdown());

            this._tray.Click += this.NotifyIconClick;
        }

        /// <summary>The notify icon click.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void NotifyIconClick(object sender, EventArgs e)
        {
            var args = e as MouseEventArgs;

            if (args == null || args.Button != MouseButtons.Left)
            {
                return;
            }

            this.Show();
            this.WindowState = WindowState.Normal;
            this.Topmost = true;
            this.Topmost = false;
        }

        /// <summary>The player window command_ on click.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void PlayerWindowCommand_OnClick(object sender, RoutedEventArgs e)
        {
            this.TabControl.SelectedIndex = 0;
        }

        /// <summary>The radio stations window command_ on click.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void RadioStationsWindowCommand_OnClick(object sender, RoutedEventArgs e)
        {
            this.TabControl.SelectedIndex = 2;
        }

        /// <summary>The settings window command_ on click.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void SettingsWindowCommand_OnClick(object sender, RoutedEventArgs e)
        {
            this.TabControl.SelectedIndex = 1;
        }

        #endregion
    }
}